package io.example.vertx;

import io.example.service.AuthorAsyncService;
import io.example.service.BookAsyncService;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.serviceproxy.ServiceBinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

/**
 * Description: SpringWorker.java .<br>
 * <p>Created Time: 2020/10/22 16:31 </p>
 *
 * @author <a href="mail to: mengxiangyuancc@gmail.com" rel="nofollow">孟祥元</a>
 */
@Component
@Scope(SCOPE_PROTOTYPE)
public class SpringWorker extends AbstractVerticle {
    private static final Logger logger = LoggerFactory.getLogger(SpringWorker.class);

    @Autowired
    private BookAsyncService bookAsyncService;

    @Autowired
    private AuthorAsyncService authorAsyncService;

    @Override
    public void start(Promise<Void> startPromise) throws Exception {
        Promise<Void> promise = Promise.promise();
        ServiceBinder binder = new ServiceBinder(vertx);
        // 注册服务代理:BookAsyncServiceImpl
        binder.setAddress(BookAsyncService.ADDRESS).register(BookAsyncService.class, bookAsyncService);
        // 注册服务代理:AuthorAsyncServiceImpl
        binder.setAddress(AuthorAsyncService.ADDRESS).register(AuthorAsyncService.class, authorAsyncService);
        logger.info("[Vertx] Service Binder Register");
        startPromise.complete();
    }
}
